package com.ftw.commons

import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.ftw.commons.BaseApplication

/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019/4/18  12:01 PM
 */

object SharedPreferenceUtils {

    private val mPreferences: SharedPreferences by lazy {
        PreferenceManager.getDefaultSharedPreferences(
            BaseApplication.instance
        )
    }

    /***
     * 保存当前账户的userId
     */
    fun saveCurrentUser(userId: String?) {
        mPreferences.edit().remove("userId").apply()
        mPreferences.edit().putString("userId", userId).apply()
    }

    /***
     * 获取当前账户的userId
     */
    fun getCurrentUser(): String? {
        return mPreferences.getString("userId", null)
    }


    fun clearCurrentUser() {
        mPreferences.edit().remove("userId").apply()
    }


    /**
     * 记住本地密码
     */
    fun saveLocalPwd(key: String, pwd: String) {
        mPreferences.edit().remove(key + "localpwd").apply()
        mPreferences.edit().putString(key + "localpwd", pwd).apply()
    }

    /**
     * 获取记住本地密码
     */
    fun getLocalPwd(key: String): String? {
        return mPreferences.getString(key + "localpwd", null)
    }

    /**
     * 清除本地密码
     */
    fun clearLocalPwd(key: String) {
        mPreferences.edit().remove(key + "localpwd").apply()
    }

    /**
     * 免责申明是否同意
     */
    fun setDisclaimer(isFlag: Boolean) {
        mPreferences.edit().remove("isDisclaimer").apply()
        mPreferences.edit().putBoolean("isDisclaimer", isFlag).apply()
    }

    /**
     * 免责申明是否同意
     */
    fun getDisclaimer(): Boolean {
        return mPreferences.getBoolean("isDisclaimer", false)
    }

    /**
     * 保存加密的私钥
     */
    fun savePrivateKeyEncryption(privateKey: String) {
        mPreferences.edit().remove("privateKeyEncry").apply()
        mPreferences.edit().putString("privateKeyEncry", privateKey).apply()
    }

    /**
     * 获取加密后的私钥
     */
    fun getPrivateKeyEncryption(): String? {
        return mPreferences.getString("privateKeyEncry", null)
    }

    /**
     * 清除加密后的私钥
     */
    fun clearPrivateKeyEncryption() {
        mPreferences.edit().remove("privateKeyEncry").apply()
    }

    /**
     * 退出登录
     */
    fun logOut() {
        mPreferences.edit().remove("isDisclaimer").apply()
        mPreferences.edit().remove("userId").apply()
    }

    /**
     * 查看邮件是否检查密码
     * true  检查密码
     * false 不检查密码
     */
    fun setCheckPassword(key: String, check: Boolean) {
        mPreferences.edit().remove(key).apply()
        mPreferences.edit().putBoolean(key, check).apply()
    }

    /**
     * 是否设置了查看邮件检查密码字段
     * true  检查密码
     * false 不检查密码
     */
    fun isCheckPassword(key: String): Boolean {
        return mPreferences.getBoolean(key, true)
    }

    /**
     * 设置邮箱列表显示（明文/密文）
     * true：密文 false：明文
     */
    fun setEmailShow(key: String, type: Boolean) {
        mPreferences.edit().remove(key + "emailShowType").apply()
        mPreferences.edit().putBoolean(key + "emailShowType", type).apply()
    }

    /**
     * 获取邮件列表显示方式（明文/密文）
     * true：密文 false：明文
     */
    fun getEmailShow(key: String): Boolean {
        return mPreferences.getBoolean(key + "emailShowType", false)
    }

    /**
     * 设置新邮件提醒
     * true：提示 false：不提示
     */
    fun setEmailPush(key: String, type: Boolean) {
        mPreferences.edit().remove(key + "emailPush").apply()
        mPreferences.edit().putBoolean(key + "emailPush", type).apply()
    }

    /**
     * 获取新邮件提醒
     * true：提示(默认) false：不提示
     */
    fun getEmailPush(key: String): Boolean {
        return mPreferences.getBoolean(key + "emailPush", true)
    }


    /**
     * x5是否初始化完成
     */
    fun isX5Init(): Boolean {
        return mPreferences.getBoolean("isInit", false)
    }

    /**
     * 设置x5初始化状态
     * true：提示 false：不提示
     */
    fun setX5Init(type: Boolean) {
        mPreferences.edit().remove("isInit").apply()
        mPreferences.edit().putBoolean("isInit", type).apply()
    }


    /**
     * 附件上传限制大小
     */
    fun saveAttachUploadMaxSize(size: Int) {
        mPreferences.edit().remove("uploadMaxSize").apply()
        mPreferences.edit().putInt("uploadMaxSize", size).apply()
    }


    /**
     * 获取附件上传限制大小
     */
    fun getAttachUploadMaxSize(): Int {
        return mPreferences.getInt("uploadMaxSize", 10)
    }

    /**
     * @name：邮件列表增量更新查询时间
     * @desc:
     * @input: key 当前登录账户，value查询时间
     * @return:
     **/
    fun saveStorageListQueryTime(key: String, timeStr: String) {
        mPreferences.edit().remove(key + "storageListQuery").apply()
        mPreferences.edit().putString(key + "storageListQuery", timeStr).apply()
    }

    /**
     * @name：清除邮件列表查询时间
     * @desc:
     * @input: key 当前登录账户
     * @return:
     **/
    fun clearStorageListQueryTime(key: String) {
        mPreferences.edit().remove(key + "storageListQuery").apply()
    }

    /**
     * @name：获取邮件列表查询时间
     * @desc:
     * @input: key 当前登录账户
     * @return:
     **/
    fun getStorageListQueryTime(key: String): String {
        return mPreferences.getString(key + "storageListQuery", "0")!!
    }


    /**
     * @name：企业通讯录增量更新查询时间
     * @desc:
     * @input: key 当前登录账户，value查询时间
     * @return:
     **/
    fun saveCompanyContactQueryTime(key: String, timeStr: String) {
        mPreferences.edit().remove(key + "companyContactQuery").apply()
        mPreferences.edit().putString(key + "companyContactQuery", timeStr).apply()
    }

    /**
     * @name：清除企业通讯录增量查询时间
     * @desc:
     * @input: key 当前登录账户
     * @return:
     **/
    fun clearCompanyContactQueryTime(key: String) {
        mPreferences.edit().remove(key + "companyContactQuery").apply()
    }

    /**
     * @name：获取企业通讯录增量查询时间
     * @desc:
     * @input: key 当前登录账户
     * @return:
     **/
    fun getCompanyContactQueryTime(key: String): String {
        return mPreferences.getString(key + "companyContactQuery", "0")!!
    }


    /**
     * 数据库版本升级完成标志
     */
    fun saveRealmDbUpdate(key: String, version: Int) {
        mPreferences.edit().remove(key + "realmDbUpdate").apply()
        mPreferences.edit().putString(key + "realmDbUpdate", version.toString())
            .apply()
    }

    /**
     * 数据库版本升级完成标志
     */
    fun getRealmDbUpdate(key: String, currentRealmVersion: String): Boolean {
        return mPreferences.getString(
            key + "realmDbUpdate",
            "-1"
        ) == currentRealmVersion
    }

    /**
     * 数据库版本升级完成标志
     */
    fun getRealmDbUpdateVersion(key: String): Int {
        return mPreferences.getString(key + "realmDbUpdate", "-1")!!.toInt()
    }

    /**
     * 保存文件默认下载位置
     */
    fun saveBoxMkdirLocation(key: String, filePath: String) {
        mPreferences.edit().remove(key + "boxMkdirLocation").apply()
        mPreferences.edit().putString(key + "boxMkdirLocation", filePath).apply()
    }

    /**
     * 获取文件默认下载位置
     */
    fun getBoxMkdirLocation(key: String): String? {
        return mPreferences.getString(key + "boxMkdirLocation", null)
    }

    /**
     * 定期清理缓存设置
     */

    fun saveRegualrStatus(key: String, status: String) {
        mPreferences.edit().remove(key + "regualr").apply()
        mPreferences.edit().putString(key + "regualr", status).apply()
    }

    /**
     * 获取文件默认下载位置
     */
    fun getRegualrStatus(key: String): String {
        return mPreferences.getString(key + "regualr", "0")!!
    }

}